#pragma once

#include "NetworkManager.h"
#include "NetworkTypes.h"
#include <functional>
#include <memory>
#include <string>
#include <thread>
#include <atomic>
#include <map>
#include "BaseNetworkChannel.h"
#include "TcpChannel.h"
#include "KcpChannel.h"
#include "TimerManager.h"

namespace SeanNetwork {

// 网络客户端类，提供给上层应用使用的接口
class NetworkClient {
public:
    NetworkClient(ProtocolType protocol = ProtocolType::TCP);
    ~NetworkClient();
    
    // 初始化客户端
    bool Initialize(const NetworkConfig& config = NetworkConfig());
    
    // 连接到服务器
    bool Connect(const std::string& host, uint16_t port);
    
    // 断开连接
    void Disconnect();
    
    // 发送消息
    bool SendMsg(std::shared_ptr<NetworkMessage> message);
    
    // 创建新消息
    std::shared_ptr<NetworkMessage> CreateMessage(uint32_t messageId, MessagePriority priority = MessagePriority::Normal);
    
    // 注册消息处理回调
    void RegisterMessageHandler(uint32_t messageId, std::function<void(std::shared_ptr<NetworkMessage>)> handler);
    
    // 注册连接状态回调
    void SetConnectionCallback(std::function<void(ConnectionState, const std::string&)> callback);
    
    // 获取连接状态
    ConnectionState GetConnectionState() const;
    
    // 获取当前通道类型
    ChannelType GetCurrentChannelType() const;
    
    // 获取网络统计信息
    NetworkStats GetNetworkStats() const;
    
    // 手动切换通道
    bool SwitchChannel(ChannelType channelType);
    
    // 启动客户端
    bool Start();
    
    // 停止客户端
    void Stop();
    
    // 设置自动重连
    void SetAutoReconnect(bool enable, uint32_t maxAttempts = 0, uint32_t initialDelayMs = 1000);
    
    // 获取协议类型
    ProtocolType GetProtocolType() const { return m_ProtocolType; }
    
    // 定时更新
    void Update(float deltaTime);
    
private:
    std::unique_ptr<NetworkManager> m_NetworkManager;
    std::map<uint32_t, std::function<void(std::shared_ptr<NetworkMessage>)>> m_MessageHandlers;
    std::function<void(ConnectionState, const std::string&)> m_ConnectionCallback;
    
    std::string m_Host;
    uint16_t m_Port;
    
    std::atomic<bool> m_AutoReconnect;
    uint32_t m_ReconnectMaxAttempts;
    uint32_t m_ReconnectInitialDelay;
    uint32_t m_ReconnectAttempts;
    
    std::unique_ptr<asio::steady_timer> m_ReconnectTimer;
    asio::io_context m_ReconnectIoContext;
    std::unique_ptr<asio::io_context::work> m_ReconnectWork;
    std::thread m_ReconnectThread;
    std::atomic<bool> m_ReconnectThreadRunning;
    
    // 处理消息
    void HandleMessage(std::shared_ptr<NetworkMessage> message);
    
    // 处理连接状态
    void HandleConnectionState(ConnectionState state, const std::string& reason);
    
    // 启动重连
    void StartReconnect();
    
    // 停止重连
    void StopReconnect();
    
    // 重连线程函数
    void ReconnectThreadFunc();
    
    // 基本属性
    ProtocolType m_ProtocolType;
    asio::io_context m_IoContext;
    std::unique_ptr<std::thread> m_IoThread;
    std::shared_ptr<asio::io_context::work> m_Work;
    
    // 网络通道
    std::shared_ptr<BaseNetworkChannel> m_NetworkChannel;
    
    // 回调函数
    MessageReceiveCallback m_MessageCallback;
    
    // 定时器管理
    std::shared_ptr<TimerManager> m_TimerManager;
    
    // 更新计时
    float m_DeltaTime;
};

} // namespace SeanNetwork
